/**
 * 定义数据传输有效率报表模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.WasteWater.dataTransValidation.TransValidateAreaController', ['water.entity.dataValidation','waterCommon','water.common.dateUtil','water.apps.WasteWater.dataTransValidation.DataValidationController']);

	module.controller('TransValidateAreaController',function($rootScope,$scope,$controller,$dateUtil,$DataReportService,$wdp,$dataValidationService,$waterDataCommon){
		
		/**
		 * 查询条件参数集
		 */
		$scope.queryObject={};
		
		$scope.allStationSelected = true;
		$scope.dataType="week";
		$scope.selectYear="";
		$scope.selectWeek="";
		$scope.selectMonth="";
		$scope.transValidateAreaInfo;
		$scope.waterDataCommon=$waterDataCommon;
		
		//改变统计周期
		$scope.dataTypeChange = function(dataType){
			$scope.dataType = dataType;
			$scope.getTransEffReport();
		}
		
		// 选择年份更新周信息
		$scope.changeYear = function(year){
			$scope.weeklist = $DataReportService.getYearWeek(year);
			$scope.selectWeek= $scope.weeklist[0].value;
			for(var i=0;i<$scope.weeklist.length;i++){
				var week = $scope.weeklist[i];
				if(week.today){
					$scope.selectWeek = week.value;
				}
			}
		}
		
		//获取传输有效率报表
		$scope.queryDataTransferRate = function(){
			$scope.queryObject = {
					"CITYCODE":"001002",
					"year":$scope.selectYear,
					"Month":$scope.selectMonth
				};

			$waterDataCommon.getPageRows("/DataValidationTransWAG/queryTransValiAreaReport.do","transValidateAreaInfo",$scope.queryObject,15,1,function(d){
				$scope.transValidateAreaInfo=d;
			});
			
		}
		
		//数据修约之后修改传输有效率
		$scope.computeDataTransferRate = function(){
			$.messager.progress({title:'提示',msg:'重新计算中',text:'努力中...',interval:'600'});
			$waterDataCommon.getPageRows("/DataValidationTrans/getTransEffReport.do","dataListInfo",$scope.queryObject,15,1,function(){
				$.messager.progress('close');
				$scope.queryDataTransferRate();//查询重新计算之后的传输有效率	
			},function(){},function(){},true); 
				
			$scope.callNoticeParmChange = setTimeout(function(){
				$.messager.progress('close');
			},120000);
		}
			
		
		//获取传输有效率报表
		$scope.exportTransEffReport = function(){
			$scope.exportTransValiAreaReport();
			$scope.exportTransValliOverSiteReport();
			$scope.exportTransValiOverNumReport();
		}
		$scope.exportTransValiAreaReport = function(){
			$scope.queryObject = {
					"CITYCODE":"001002",
					"year":$scope.selectYear,
					"Month":$scope.selectMonth
				};
			$dataValidationService.exportTransValiAreaReport($scope.queryObject);
		}
		$scope.exportTransValliOverSiteReport = function(){
			$scope.queryObject = {
					"CITYCODE":"001002",
					"year":$scope.selectYear,
					"Month":$scope.selectMonth
				};
			$dataValidationService.exportTransValliOverSiteReport($scope.queryObject);
		}
		$scope.exportTransValiOverNumReport = function(){
			$scope.queryObject = {
					"CITYCODE":"001002",
					"year":$scope.selectYear,
					"Month":$scope.selectMonth
				};
			$dataValidationService.exportTransValiOverNumReport($scope.queryObject);
		}
		
		$scope.onManualClick = function() {
			 $scope.manual=true;
			 $scope.getMonitorDataValidationStatisticsTotal();
		 }
		
		// 在线监测数据有效性统计
		$scope.validationList = [];
		$scope.getMonitorDataValidationStatisticsTotal=function(flag){
			if(flag=='0') {
				$scope.manual=false;
			}
			//查询停电时间并重新计算
			$scope.queryNoPowerTimeByStationID();
		}
		 
		$scope.retry = function(){
			var start_time="";
			var end_time="";
			var flag = /\//g;
			var startArray=[];
			var endArray=[];
			if($scope.noPowerList.length>0) 
			{
				for(var k =0;k<$scope.noPowerList.length;k++) 
				{
					var st_index=0;
					var end_index=0;
					if($scope.noPowerList[k].NO_POWER_START_TIME==null ||$scope.noPowerList[k].isNew==true) 
					{
						continue;
					}
					if($scope.noPowerList[k].NO_POWER_END_TIME==null) 
					{
						continue;
					}
					st_index = $scope.noPowerList[k].NO_POWER_START_TIME.indexOf("/");
					end_index = $scope.noPowerList[k].NO_POWER_END_TIME.indexOf("/");
					if(st_index>0) 
					{
						$scope.noPowerList[k].NO_POWER_START_TIME = $scope.noPowerList[k].NO_POWER_START_TIME.replace(flag,"-");
						$scope.noPowerList[k].NO_POWER_START_TIME=$scope.noPowerList[k].NO_POWER_START_TIME+":00";
					}
					if(end_index>0) 
					{
						$scope.noPowerList[k].NO_POWER_END_TIME = $scope.noPowerList[k].NO_POWER_END_TIME.replace(flag,"-");
						$scope.noPowerList[k].NO_POWER_END_TIME=$scope.noPowerList[k].NO_POWER_END_TIME+":00";
					}
					start_time += $scope.noPowerList[k].NO_POWER_START_TIME+",";
					startArray.push($scope.noPowerList[k].NO_POWER_START_TIME);
					end_time += $scope.noPowerList[k].NO_POWER_END_TIME+",";
					endArray.push($scope.noPowerList[k].NO_POWER_END_TIME);
				}
			}
			$scope.queryObject.STATION_ID=$wdp.viewValue.wwtpDetailParm.STATION_ID;
			$scope.queryObject.ERROR_START_TIME=start_time;
			$scope.queryObject.ERROR_END_TIME=end_time;
			
			$scope.validationList = [];		
			$scope.queryObject.HOUR="1";
			if($scope.manual==true) 
			{
				var flag = checkTimeOverLap(startArray,endArray);
				if(flag==false) 
				{
					return;
				}
				flag = checkTimeDouble(null,$scope.noPowerList);
				if(flag==false) 
				{
					return;
				}
				$scope.computeDataTransferRate($scope.queryObject);//计算传输有效率
			} 
			else 
			{
				$scope.queryDataTransferRate();//查询传输有效率
			}
		}
		
		$scope.queryNoPowerTimeByStationID = function() {
			 $scope.noPowerList="";
			 $scope.queryObject.STATION_ID=$wdp.viewValue.wwtpDetailParm.STATION_ID;
				$scope.queryObject.START_TIME =$scope.selectYear + "-" + $scope.selectMonth + "-01" + " 00:00:00";
				//获取每个月最后一天
				var end_day = new Date($scope.selectYear,$scope.selectMonth,0); 
				$scope.queryObject.END_TIME =$scope.selectYear + "-" + $scope.selectMonth + "-"+end_day.getDate() + " 23:59:59";
				$scope.queryObject.DATA_TYPE = $scope.dataType;
				if($scope.dataType=="week"){
					var weekInfo=$scope.selectWeek.split(",");
					$scope.queryObject.START_TIME = weekInfo[0] + " 00:00:00";
					$scope.queryObject.END_TIME = weekInfo[1] + " 23:59:59";
				}
				$waterDataCommon.getPageRows("/IndusteryPollutQuarterDataValidation/queryNoPowerByStationId.do","noPowerList",$scope.queryObject,100,1,function(d){
					if(d) {
						var length = d.data.length;
						var timeNameArray=[];
						if(length>0){
							for(var i =0;i<d.data.length;i++) {
								var name = d.data[i].TIME_NUM;
								name = name.substring(4,name.length);
								timeNameArray.push(parseInt(name));
							}
							timeNameArray.sort(sortNumber);
							length = parseInt(timeNameArray[length-1])+parseInt(1);
							d.data.push({isNew:true,MN:$scope.queryObject.STATION_ID,TIME_NUM:'停电时段'+length});
						} else {
							d.data.push({isNew:true,MN:$scope.queryObject.STATION_ID,TIME_NUM:'停电时段1'});
						}
						$scope.noPowerList=d.data;
						$scope.retry();
					}
				});
		}
		function sortNumber(a,b) 
		{ 
		return a - b 
		}
		//验证时间是否存在重叠
		function checkTimeOverLap(startArray,endArray) {
		    if(startArray==null || startArray.length<=1) {
		    	return true;
		    }
			var startTimeArr = [];
		    var endTimeArr = [];
		    startTimeArr=startArray;
		    endTimeArr = endArray;
		    var timeE = '',timeS = '';
		    var startTimeArr = startTimeArr.sort();
		    var endTimeArr = endTimeArr.sort();
		    for(var k=1;k<startTimeArr.length;k++){
		        if (startTimeArr[k] <endTimeArr[k-1]){
		            $.messager.alert("提示","时间段存在重叠");
		            return false;
		        }
		    }
		    return true;
		}
		//验证时间是否存在重复
		function checkTimeDouble(newTime,oldTimeArray) {
			var startTime ="";
		    var endTime = "";
		    if(newTime==null ||newTime.length==0) {
		    	return true;
		    }
		    startTime = newTime.NO_POWER_START_TIME;
		    endTime = newTime.NO_POWER_END_TIME;
		    if(oldTimeArray==null || oldTimeArray.length<=1) {
		    	return true;
		    }
		    for(var k=0;k<oldTimeArray.length;k++){
		    	if(oldTimeArray[k].isNew==true) {
		    		continue;
		    	}
		    	var start1 = oldTimeArray[k].NO_POWER_START_TIME;
		    	var start2 = oldTimeArray[k+1].NO_POWER_START_TIME;
		    	var end1 = oldTimeArray[k].NO_POWER_END_TIME;
		    	var end2 = oldTimeArray[k+1].NO_POWER_END_TIME;
		    	if(start1==start2 && end1==end2) {
		            $.messager.alert("提示","时间段重复");
		            return false;
		    	}
		    }
		    return true;
		}
		
		/**
		 * 监听左侧列表选择
		 */
		$scope.oNdataPramChanged = $scope.$on('dataPramChanged', function(event, station) {
			$scope.selectedModel = station;
	    });
		
		
		/**
		 * 初始化
		 */
		function _init(){
			$scope.yearlist=[];
			var currentYear=(new Date()).getFullYear();
			for(var i=0;i<4;i++){
				$scope.yearlist.push({
					name:currentYear-i+"年",
					value:currentYear-i+""
				});
			}; 
			$scope.selectYear=currentYear+"";
			$scope.changeYear($scope.selectYear);
			$scope.selectMonth=(new Date()).getMonth()+1;
			
			//页面执行初，oNdataPramChanged没有及时起到作用,先预留，后续解决
			if($wdp.viewValue.wwtpDetailParm){
				$scope.selectedModel=$wdp.viewValue.wwtpDetailParm;
			}
		}
		
		_init();
		
	});
}(window, angular));	